package com.tengju.bff.interfaces.data;

import com.tengju.bff.interfaces.data.settle.SettleJobListener;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.servlet.NotTokenValid;
import com.tengju.data.application.profit.MarketBonusService;
import com.tengju.data.domain.model.report.AchievementBonusCalConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author chenhang
 * @Date 2020/12/22
 */
@Validated
@RestController
@Api(tags = "绩效奖励")
@RequestMapping(value = "data/bonus/")
@Slf4j
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class AchievementBonusCalController {

    private final MarketBonusService marketBonusService;
    private final AchievementBonusCalConfig calConfig;
    private final SettleJobListener settleJobListener;

    @Value("${system.env}")
    String env;


    @ApiOperation(value = "执行月度任务", notes = "执行月度任务")
    @RequestMapping(value = "execMonthSnapshotJob", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @NotTokenValid
    public ApiResponse<Boolean> execMonthSnapshotJob(@RequestParam(required = false) Integer month) {
            marketBonusService.calMonthMarketBonus(month);
            return ApiResponse.newSuccess(true);

    }

    @ApiOperation(value = "执行小时任务", notes = "执行小时任务")
    @RequestMapping(value = "execRealtimeJob", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @NotTokenValid
    public ApiResponse<Boolean> execMonthSnapshotJob() {
            marketBonusService.calRealtimeMarketBonus();
            return ApiResponse.newSuccess(true);
    }

    @ApiOperation(value = "风控对比月度任务", notes = "风控对比月度任务")
    @RequestMapping(value = "execMonthRiskJob", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @NotTokenValid
    public ApiResponse<Boolean> execMonthRiskJob(@RequestParam(required = false) Integer month) {
        marketBonusService.calMonthMarketBonusRisk(month);
        return ApiResponse.newSuccess(true);
    }

    @ApiOperation(value = "月度星级快照任务", notes = "月度星级快照任务")
    @RequestMapping(value = "execStarMonthSnapshotJob", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @NotTokenValid
    public ApiResponse<Boolean> execStarMonthSnapshotJob(@RequestParam Integer month) {
            marketBonusService.saveStarMonthSnapshot(month);
            return ApiResponse.newSuccess(true);
    }

    @ApiOperation(value = "更新特殊名单日期阈值", notes = "更新特殊名单日期阈值")
    @RequestMapping(value = "updateMonth", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @NotTokenValid
    public ApiResponse<String> updateMonth(@RequestParam Integer month) {
            if (env.equals("prod")) {
                return ApiResponse.newSuccess("线上环境不允许修改");
            }
            calConfig.setSpecialStarYearMonthThreshold(month);
            return ApiResponse.newSuccess("success");
    }

    @ApiOperation(value = "单个用户入账结算", notes = "单个用户入账结算")
    @RequestMapping(value = "settleMonth", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @NotTokenValid
    public ApiResponse<String> updateMonth(@RequestParam Integer month,
                                           @RequestParam Long idCode) {
        settleJobListener.doSettleMonth(month, List.of(idCode));
        return ApiResponse.newSuccess("success");
    }

}
